package graph.label;
import graph.GraphPanel;
import graph.element.element;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;


public class Label extends element {
	public String text;
	public Direction direction;
	public Label(GraphPanel graphingData, int x, int y, Direction direction,  String text) {
		super(graphingData);
		FontMetrics fm = graphingData.getG().getFontMetrics();
		int width = fm.stringWidth(text);
		if (direction == Direction.LEFT) {
			this.x = x - width - 2*SPACE;
			this.y = y;
		} else if (direction == Direction.DOWN) {
			this.x = x;
			this.y = y + width + 2*SPACE;
		}
		this.direction = direction;
		this.text = text;
	}
	public Direction getDirection() {
		return direction;
	}
	public String getText() {
		return text;
	}
	public void draw() {
		Graphics g = graphingData.getG();
		FontMetrics fm = g.getFontMetrics();
		int height = fm.getHeight();
		if (direction == Direction.DOWN) {
			Graphics2D g2d = (Graphics2D) g;
			AffineTransform fontAT = new AffineTransform();
			Font theFont = g2d.getFont();
			fontAT.rotate(-Math.PI / 2.0);
			Font theDerivedFont = theFont.deriveFont(fontAT);
			g2d.setFont(theDerivedFont);
			g2d.drawString(getText(), getX() + height/3, getY());
			g2d.setFont(theFont);
		} else {
			g.drawString(getText(), getX(), getY()+height/3);
		}
	}
}
